package be.vdab.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.vdab.dao.SauzenDAO;

/**
 * Servlet implementation class SauzenVerwijderenServlet
 */
@WebServlet("/sauzen/verwijderen")
public class SauzenVerwijderenServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static final String VIEW = "/sauzen";
    private static final String BUILD_VIEW = "/WEB-INF/JSP/verwijderen.jsp";
    private static final SauzenDAO sauzenDAO = new SauzenDAO();
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		request.setAttribute("sauzen", sauzenDAO.findAll());
		RequestDispatcher dispatcher = request.getRequestDispatcher(BUILD_VIEW);
		dispatcher.forward(request, response);
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		if (request.getParameter("nummer") != null){
			try{
				for (String nummerAlsString : request.getParameterValues("nummer")) {
					long nummer = Long.parseLong(nummerAlsString);
		        	sauzenDAO.delete(nummer);
		        }
			} catch (Exception ex) {
				
			}
		}
		response.sendRedirect(request.getContextPath() + VIEW);
	}
}
